package com.skywolf.chem.utils.file;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
import com.skywolf.chem.utils.SysConfig;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by pei_pei on 2020/6/29.
 */
@Configuration
public class UploadFile {

    public static void upload(InputStream file,String pathFile)throws IOException  {
        String accessKeyId = SysConfig._this.getProperty("aliyunfile.accessKeyId");
        String accessSecret = SysConfig._this.getProperty("aliyunfile.accessSecret");
        String bucketName = SysConfig._this.getProperty("aliyunfile.bucketName");
        String endpoint = SysConfig._this.getProperty("aliyunfile.endpoint");
        String[] fo = new String[] { "", "" };
        // Endpoint以杭州为例，其它Region请按实际情况填写。
//        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
//        String accessKeyId = "LTAI4G6TYZwMhMnwCDzqRTB5";
//        String accessSecret = "nv9ly51gDTG8F0ZWpYCDS3tVoh78iZ";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessSecret);
        // 创建PutObjectRequest对象。
//        PutObjectRequest putObjectRequest = new PutObjectRequest("<yourBucketName>", "<yourObjectName>", file);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, pathFile, file);

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        // ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);
        // putObjectRequest.setMetadata(metadata);
        // 上传文件。
        ossClient.putObject(putObjectRequest);
        // 关闭OSSClient。
        ossClient.shutdown();
    }

}
